package com.c2soft.nepalcmx.controller;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j2;

import org.apache.log4j.Category;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.c2soft.nepalcmx.command.AccountUserCommand;
import com.c2soft.nepalcmx.command.BrokerFormCommand;
import com.c2soft.nepalcmx.command.NetPositionCommand;
import com.c2soft.nepalcmx.command.OrderCommand;
import com.c2soft.nepalcmx.command.TradeBookCommand;
import com.c2soft.nepalcmx.services.BrokerFormService;
import com.c2soft.nepalcmx.services.UserFormService;
import com.c2soft.nepalcmx.util.JsonUtil;

@Log4j2
@Controller
@RequestMapping("/user")
public class UserFormController {

	@Autowired
	private UserFormService userFormService;
	@Autowired
	private BrokerFormService brokerFormService;
	
	
	@RequestMapping(value = "/registration", method=RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public String registration( @RequestBody String json,HttpServletRequest req) {
		String brokerId=(String) req.getSession().getAttribute("UsrId");
        BrokerFormCommand registration= (BrokerFormCommand)JsonUtil.fromJson(json,BrokerFormCommand.class); 
        registration.setbrokerId(brokerId);
		brokerFormService.registrationUserService(registration);
		return "{\"msg\":\"User is Registered successfully.\"}";
	}
	
	@RequestMapping(value = "/userDetail",method=RequestMethod.GET, headers = "Accept=application/json")
	
	public @ResponseBody List<BrokerFormCommand> getUserForm(HttpServletRequest req) {
		String brokerId=(String) req.getSession().getAttribute("UsrId");
		List<BrokerFormCommand> userdetail=brokerFormService.getUserForm(brokerId);
		return userdetail;
	}

@RequestMapping(value = "/UpdatePwd", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String update( @RequestBody String json,HttpServletRequest req) {
	String userId=(String) req.getSession().getAttribute("UsrId");
	BrokerFormCommand update= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
    update.setUserId(userId);
	userFormService.updateService(update);
	return "{\"msg\":\"Password updated successfully.\"}";
}

@RequestMapping(value = "/UpdatePwd1", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String update1( @RequestBody BrokerFormCommand user,HttpServletRequest req) {
	userFormService.updateService(user);
	return "success";
}

@RequestMapping(value = "/UpdatePwd2", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String update2( @RequestBody BrokerFormCommand user,HttpServletRequest req) {
	userFormService.updateBrokerService(user);
	return "success";
}

@RequestMapping(value = "/UpdateBrokePwd", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdateBrokePwd( @RequestBody String json,HttpServletRequest req) {
	String brokerId=(String) req.getSession().getAttribute("UsrId");
	BrokerFormCommand update= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
    update.setbrokerId(brokerId);
    userFormService.updateBrokerService(update);
	return "{\"msg\":\"Password updated successfully.\"}";
}

@RequestMapping(value = "/UpdatePwdX", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdatePwdX( @RequestBody String json) {
	BrokerFormCommand update= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
    userFormService.updateExchangeService(update);
	return "{\"msg\":\"Password updated successfully.\"}";
}
@RequestMapping(value = "/UpdateBrokeTransPwd", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdateBrokeTransPwd( @RequestBody String json,HttpServletRequest req) {
	String brokerId=(String) req.getSession().getAttribute("UsrId");
	BrokerFormCommand update= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
    update.setbrokerId(brokerId);
    userFormService.updateBrokerTransPwdService(update);
	return "{\"msg\":\"Transaction Password updated successfully.\"}";
}

@RequestMapping(value = "/UpdateTransPwdX", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdateTransPwdX( @RequestBody String json,HttpServletRequest req) {
	BrokerFormCommand update= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
	userFormService.updateExchangeTransPwdService(update);
	return "{\"msg\":\"Transaction Password updated successfully.\"}";
}
@RequestMapping(value = "/UpdatePwd3", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String update3( @RequestBody BrokerFormCommand user) {
	userFormService.updateExchangeService(user);
	return "success";
}

@RequestMapping(value = "/UpdateTransPwd1", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdateTransPwd( @RequestBody BrokerFormCommand user,HttpServletRequest req) {
	userFormService.updateTransPwdService(user);
	return "success";
}

@RequestMapping(value = "/UpdateTransPwd2", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdateTransPwd2( @RequestBody BrokerFormCommand user,HttpServletRequest req) { 
	userFormService.updateBrokerTransPwdService(user);
	return "success";
}

@RequestMapping(value = "/UpdateTransPwd3", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String UpdateTransPwd3( @RequestBody BrokerFormCommand user,HttpServletRequest req) {
	userFormService.updateExchangeTransPwdService(user);
	return "success";
}

@RequestMapping(value = "/UpdateTransPwd", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String updateTransPwd( @RequestBody String json,HttpServletRequest req) {
	BrokerFormCommand update= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
	userFormService.updateTransPwdService(update);
	return "{\"msg\":\"Transaction Password updated successfully.\"}";
}

@RequestMapping(value = "/selectUser",method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")
public @ResponseBody List<BrokerFormCommand> updateUserData(HttpServletRequest req,@RequestBody String userId) {
	List<BrokerFormCommand> users=userFormService.getUserList(userId);
	return users;

}
@RequestMapping(value = "/updateUser", method=RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public String updateAccount( @RequestBody String json) {
	BrokerFormCommand user= (BrokerFormCommand) JsonUtil.fromJson(json,BrokerFormCommand.class);
    userFormService.updateUserService(user);
	return "{\"msg\":\"User's Password updated successfully.\"}";
}

@RequestMapping(value = "/updateUserAcc", method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")
@ResponseBody
public String updateUserAccount( HttpServletRequest req) {
	String userId=req.getParameter("id");
	String fname=req.getParameter("fname");
	String lname=req.getParameter("lname");
	String emailID=req.getParameter("emailID");
	String phone=req.getParameter("phone");

	BrokerFormCommand user=new BrokerFormCommand();
	user.setUserId(userId);
	user.setEmailID(emailID);
	user.setFname(fname);
	user.setLname(lname);
	user.setPhone(phone);
	userFormService.updateUserAccount(user);
	return "{\"msg\":\"success\"}";
}

@RequestMapping(value = "/updateBrokerAcc", method={RequestMethod.POST,RequestMethod.GET}, headers = "Accept=application/json")
@ResponseBody
public String updateBrokerAccount( HttpServletRequest req) {
	
	String brokerId=req.getParameter("id");
	String fname=req.getParameter("fname");
	String lname=req.getParameter("lname");
	String emailID=req.getParameter("emailID");
	String phone=req.getParameter("phone");
	BrokerFormCommand user=new BrokerFormCommand();
	user.setbrokerId(brokerId);
	user.setEmailID(emailID);
	user.setFname(fname);
	user.setLname(lname);
	user.setPhone(phone);
	userFormService.updateBrokerAccount(user);
	return "{\"msg\":\"success\"}";
}

@RequestMapping(value = "/getUserDetail", method=RequestMethod.GET,headers = "Accept=application/json")
public @ResponseBody List<AccountUserCommand> getUserDetail( HttpServletRequest req) {
	String userId=(String) req.getSession().getAttribute("UsrId");
	List<AccountUserCommand> accUser=userFormService.getUsersDetail(userId);
	return accUser;
}

@RequestMapping(value = "/getUserDetail1", method=RequestMethod.GET,headers = "Accept=application/json")
public @ResponseBody List<AccountUserCommand> getUserDetail1( @RequestParam("userId") String userId) {
	List<AccountUserCommand> accUser=userFormService.getUsersDetail(userId);
	return accUser;
}

@RequestMapping(value = "/getBrokerDetail", method=RequestMethod.GET,headers = "Accept=application/json")
public @ResponseBody List<AccountUserCommand> getBrokerDetail( HttpServletRequest req) {
	String brokerId=(String) req.getSession().getAttribute("UsrId");
	List<AccountUserCommand> accUser=userFormService.getBrokersDetail(brokerId);
	return accUser;
}



@RequestMapping(value = "/RMSPortal", method = { RequestMethod.GET,
		RequestMethod.POST }, produces = "application/json")
public void rmsPortal( @RequestBody BrokerFormCommand user,HttpServletRequest req) {
	String brokerId=(String) req.getSession().getAttribute("UsrId");
	user.setbrokerId(brokerId);
	userFormService.postRMSNotificationForUser(user);
}

@RequestMapping(value = "/ExRMSPortal", method = { RequestMethod.GET,
		RequestMethod.POST }, produces = "application/json")
public void rmsPortalX( @RequestBody BrokerFormCommand broker,HttpServletRequest req) {
	
	userFormService.postRMSNotificationForBroker(broker);
}

@RequestMapping(value = "/rmsNewsForUser",method=RequestMethod.GET, headers = "Accept=application/json")

public @ResponseBody List<BrokerFormCommand> rmsNewsForUser(@RequestParam("userId") String userId) {
	List<BrokerFormCommand> rms=userFormService.getRMSReportForUser(userId);
return rms;
}

@RequestMapping(value = "/rmsNewsForBroker",method=RequestMethod.GET, headers = "Accept=application/json")

public @ResponseBody List<BrokerFormCommand> rmsNewsForBroker(@RequestParam("brokerId") String brokerId) {
	List<BrokerFormCommand> rms=userFormService.getRMSReportForBroker(brokerId);
return rms;
}



@RequestMapping(value = "/updateStatus",method={RequestMethod.GET,RequestMethod.POST}, headers = "Accept=application/json")
public  void setStatus(@RequestParam("Id") String Id)
{
	userFormService.updateStatus(Id);	
	}


@RequestMapping(value = "/updateStatus1",method={RequestMethod.GET,RequestMethod.POST}, headers = "Accept=application/json")
public @ResponseBody List<BrokerFormCommand> setStatus1(@RequestParam("UserId") String userId)
	{
	List<BrokerFormCommand> rms=userFormService.updateStatus1(userId);
	return rms;
	}

@RequestMapping(value = "/updateBrokerStatus1",method={RequestMethod.GET,RequestMethod.POST}, headers = "Accept=application/json")
public @ResponseBody List<BrokerFormCommand> setBrokerStatus1(@RequestParam("brokerId") String brokerId)
	{
	List<BrokerFormCommand> rms=userFormService.updateStatusBroker1(brokerId);
	return rms;
	}


@RequestMapping(value = "/ForgetPwd", method=RequestMethod.POST, headers = "Accept=application/json")

public void forgetPwd( @RequestBody String json) {
	
	BrokerFormCommand forgetpwd= (BrokerFormCommand)JsonUtil.fromJson(json,BrokerFormCommand.class); 
    userFormService.forgetPwdService(forgetpwd);
	
}

@RequestMapping(value = "/ForgetPwd1",method=RequestMethod.POST, headers = "Accept=application/json")

public void ForgetPwd(@RequestBody BrokerFormCommand forgetpwd) {
	userFormService.forgetPwdService(forgetpwd);
}

}

